<template>
	<main-container>
		<!-- 搜索 -->
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="手机号">
				<el-input v-model="queryParams.phone" clearable></el-input>
			</el-form-item>
			<el-form-item label="应用ID">
				<el-input v-model="queryParams.appId" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage"
					><el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery"
					><el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div></div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="tableData" border>
			<el-table-column prop="appId" label="应用ID"></el-table-column>
			<el-table-column prop="openid" label="openid"></el-table-column>
			<el-table-column prop="sessionKey" label="sessionKey"></el-table-column>

			<el-table-column prop="avatarUrl" label="头像">
				<template #default="scope">
					<el-image
						v-if="scope.row.avatarUrl"
						style="width: 50px; height: 50px"
						:src="scope.row.avatarUrl"
						:preview-src-list="[scope.row.avatarUrl]"
						fit="cover"
						:preview-teleported="true"
					/>
				</template>
			</el-table-column>
			<el-table-column prop="nickName" label="昵称"></el-table-column>
			<el-table-column prop="phone" label="手机号" />
			<el-table-column prop="createTime" label="创建时间" />
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="page.total"
			v-model:currentPage="page.currentPage"
			v-model:pageSize="page.pageSize"
			@changePage="initPage"
		></pagination>
	</main-container>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs } from 'vue';
import { getPage } from '/@/api/miniapp/weixin/wxuser';
export default defineComponent({
	setup() {
		const data = reactive({
			queryParams: { phone: '', appId: '' },
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 10, // 每页显示多少条
				ascs: [],
				descs: 'create_time',
			},
			tableData: [],
		});
		const showSearch = ref(true);
		const loading = ref(false);

		const initPage = async () => {
			loading.value = true;
			let params = {
				current: data.page.currentPage,
				size: data.page.pageSize,
				ascs: data.page.ascs,
				descs: data.page.descs,
			};
			await getPage(Object.assign(params, data.queryParams))
				.then((response) => {
					data.tableData = response.data.data.records;
					data.page.total = response.data.data.total;
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 重置搜索表单
		 */
		const resetQuery = () => {
			data.queryParams = { phone: '', appId: '' };
		};

		initPage();
		return {
			loading,
			showSearch,
			initPage,
			resetQuery,
			...toRefs(data),
		};
	},
});
</script>
